Mercurial > vim
annotate src/regexp_nfa.c @ 4529:432a6b8c7d93 v7.3.1012
updated for version 7.3.1012
Problem: \Z does not work properly with the new regexp engine.
Solution: Make \Z work. Add tests.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Fri, 24 May 2013 23:10:50 +0200 |
parents | 55bcaa1d2749 |
children | 1be43c095aff |
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; | |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1187 } /* if exists closing ] */ |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1188 |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1189 if (reg_strict) |
4444 | 1190 { |
1191 syntax_error = TRUE; | |
1192 EMSG_RET_FAIL(_(e_missingbracket)); | |
1193 } | |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1194 /* FALLTHROUGH */ |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1195 |
4444 | 1196 default: |
1197 { | |
1198 #ifdef FEAT_MBYTE | |
1199 int plen; | |
1200 | |
1201 nfa_do_multibyte: | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1202 /* Length of current char with composing chars. */ |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1203 if (enc_utf8 && clen != (plen = (*mb_ptr2len)(old_regparse))) |
4444 | 1204 { |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1205 /* A base character plus composing characters. |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1206 * 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
|
1207 * 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
|
1208 * NFA_END_COMPOSING is the ). Note that right now we are |
4444 | 1209 * building the postfix form, not the NFA itself; |
1210 * 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
|
1211 * 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
|
1212 i = 0; |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1213 for (;;) |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1214 { |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1215 EMIT(c); |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1216 if (i > 0) |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1217 EMIT(NFA_CONCAT); |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
1218 if ((i += utf_char2len(c)) >= plen) |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1219 break; |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1220 c = utf_ptr2char(old_regparse + i); |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1221 } |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
1222 EMIT(NFA_COMPOSING); |
4444 | 1223 regparse = old_regparse + plen; |
1224 } | |
1225 else | |
1226 #endif | |
1227 { | |
1228 c = no_Magic(c); | |
1229 EMIT(c); | |
1230 } | |
1231 return OK; | |
1232 } | |
1233 } | |
1234 | |
1235 #undef TRY_NEG | |
1236 #undef EMIT_GLUE | |
1237 | |
1238 return OK; | |
1239 } | |
1240 | |
1241 /* | |
1242 * Parse something followed by possible [*+=]. | |
1243 * | |
1244 * A piece is an atom, possibly followed by a multi, an indication of how many | |
1245 * times the atom can be matched. Example: "a*" matches any sequence of "a" | |
1246 * characters: "", "a", "aa", etc. | |
1247 * | |
1248 * piece ::= atom | |
1249 * or atom multi | |
1250 */ | |
1251 static int | |
1252 nfa_regpiece() | |
1253 { | |
1254 int i; | |
1255 int op; | |
1256 int ret; | |
1257 long minval, maxval; | |
1258 int greedy = TRUE; /* Braces are prefixed with '-' ? */ | |
1259 char_u *old_regparse, *new_regparse; | |
1260 int c2; | |
1261 int *old_post_ptr, *my_post_start; | |
1262 int old_regnpar; | |
1263 int quest; | |
1264 | |
1265 /* Save the current position in the regexp, so that we can use it if | |
1266 * <atom>{m,n} is next. */ | |
1267 old_regparse = regparse; | |
1268 /* Save current number of open parenthesis, so we can use it if | |
1269 * <atom>{m,n} is next */ | |
1270 old_regnpar = regnpar; | |
1271 /* store current pos in the postfix form, for \{m,n} involving 0s */ | |
1272 my_post_start = post_ptr; | |
1273 | |
1274 ret = nfa_regatom(); | |
1275 if (ret == FAIL) | |
1276 return FAIL; /* cascaded error */ | |
1277 | |
1278 op = peekchr(); | |
1279 if (re_multi_type(op) == NOT_MULTI) | |
1280 return OK; | |
1281 | |
1282 skipchr(); | |
1283 switch (op) | |
1284 { | |
1285 case Magic('*'): | |
1286 EMIT(NFA_STAR); | |
1287 break; | |
1288 | |
1289 case Magic('+'): | |
1290 /* | |
1291 * Trick: Normally, (a*)\+ would match the whole input "aaa". The | |
1292 * first and only submatch would be "aaa". But the backtracking | |
1293 * engine interprets the plus as "try matching one more time", and | |
1294 * a* matches a second time at the end of the input, the empty | |
1295 * string. | |
1296 * The submatch will the empty string. | |
1297 * | |
1298 * In order to be consistent with the old engine, we disable | |
1299 * NFA_PLUS, and replace <atom>+ with <atom><atom>* | |
1300 */ | |
1301 /* EMIT(NFA_PLUS); */ | |
1302 regnpar = old_regnpar; | |
1303 regparse = old_regparse; | |
1304 curchr = -1; | |
1305 if (nfa_regatom() == FAIL) | |
1306 return FAIL; | |
1307 EMIT(NFA_STAR); | |
1308 EMIT(NFA_CONCAT); | |
1309 skipchr(); /* skip the \+ */ | |
1310 break; | |
1311 | |
1312 case Magic('@'): | |
1313 op = no_Magic(getchr()); | |
1314 switch(op) | |
1315 { | |
1316 case '=': | |
1317 EMIT(NFA_PREV_ATOM_NO_WIDTH); | |
1318 break; | |
1319 case '!': | |
1320 case '<': | |
1321 case '>': | |
1322 /* Not supported yet */ | |
1323 return FAIL; | |
1324 default: | |
1325 syntax_error = TRUE; | |
4450 | 1326 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
4444 | 1327 return FAIL; |
1328 } | |
1329 break; | |
1330 | |
1331 case Magic('?'): | |
1332 case Magic('='): | |
1333 EMIT(NFA_QUEST); | |
1334 break; | |
1335 | |
1336 case Magic('{'): | |
1337 /* a{2,5} will expand to 'aaa?a?a?' | |
1338 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy | |
1339 * version of '?' | |
1340 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the | |
1341 * parenthesis have the same id | |
1342 */ | |
1343 | |
1344 greedy = TRUE; | |
1345 c2 = peekchr(); | |
1346 if (c2 == '-' || c2 == Magic('-')) | |
1347 { | |
1348 skipchr(); | |
1349 greedy = FALSE; | |
1350 } | |
1351 if (!read_limits(&minval, &maxval)) | |
1352 { | |
1353 syntax_error = TRUE; | |
1354 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); | |
1355 } | |
1356 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to | |
1357 * <atom>* */ | |
1358 if (minval == 0 && maxval == MAX_LIMIT && greedy) | |
1359 { | |
1360 EMIT(NFA_STAR); | |
1361 break; | |
1362 } | |
1363 | |
1364 if (maxval > NFA_BRACES_MAXLIMIT) | |
1365 { | |
1366 /* This would yield a huge automaton and use too much memory. | |
1367 * Revert to old engine */ | |
1368 return FAIL; | |
1369 } | |
1370 | |
1371 /* Special case: x{0} or x{-0} */ | |
1372 if (maxval == 0) | |
1373 { | |
1374 /* Ignore result of previous call to nfa_regatom() */ | |
1375 post_ptr = my_post_start; | |
1376 /* NFA_SKIP_CHAR has 0-length and works everywhere */ | |
1377 EMIT(NFA_SKIP_CHAR); | |
1378 return OK; | |
1379 } | |
1380 | |
1381 /* Ignore previous call to nfa_regatom() */ | |
1382 post_ptr = my_post_start; | |
1383 /* Save pos after the repeated atom and the \{} */ | |
1384 new_regparse = regparse; | |
1385 | |
1386 new_regparse = regparse; | |
1387 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); | |
1388 for (i = 0; i < maxval; i++) | |
1389 { | |
1390 /* Goto beginning of the repeated atom */ | |
1391 regparse = old_regparse; | |
1392 curchr = -1; | |
1393 /* Restore count of parenthesis */ | |
1394 regnpar = old_regnpar; | |
1395 old_post_ptr = post_ptr; | |
1396 if (nfa_regatom() == FAIL) | |
1397 return FAIL; | |
1398 /* after "minval" times, atoms are optional */ | |
1399 if (i + 1 > minval) | |
1400 EMIT(quest); | |
1401 if (old_post_ptr != my_post_start) | |
1402 EMIT(NFA_CONCAT); | |
1403 } | |
1404 | |
1405 /* Go to just after the repeated atom and the \{} */ | |
1406 regparse = new_regparse; | |
1407 curchr = -1; | |
1408 | |
1409 break; | |
1410 | |
1411 | |
1412 default: | |
1413 break; | |
1414 } /* end switch */ | |
1415 | |
1416 if (re_multi_type(peekchr()) != NOT_MULTI) | |
1417 { | |
1418 /* Can't have a multi follow a multi. */ | |
1419 syntax_error = TRUE; | |
1420 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); | |
1421 } | |
1422 | |
1423 return OK; | |
1424 } | |
1425 | |
1426 /* | |
1427 * Parse one or more pieces, concatenated. It matches a match for the | |
1428 * first piece, followed by a match for the second piece, etc. Example: | |
1429 * "f[0-9]b", first matches "f", then a digit and then "b". | |
1430 * | |
1431 * concat ::= piece | |
1432 * or piece piece | |
1433 * or piece piece piece | |
1434 * etc. | |
1435 */ | |
1436 static int | |
1437 nfa_regconcat() | |
1438 { | |
1439 int cont = TRUE; | |
1440 int first = TRUE; | |
1441 | |
1442 while (cont) | |
1443 { | |
1444 switch (peekchr()) | |
1445 { | |
1446 case NUL: | |
1447 case Magic('|'): | |
1448 case Magic('&'): | |
1449 case Magic(')'): | |
1450 cont = FALSE; | |
1451 break; | |
1452 | |
1453 case Magic('Z'): | |
1454 #ifdef FEAT_MBYTE | |
1455 regflags |= RF_ICOMBINE; | |
1456 #endif | |
1457 skipchr_keepstart(); | |
1458 break; | |
1459 case Magic('c'): | |
1460 regflags |= RF_ICASE; | |
1461 skipchr_keepstart(); | |
1462 break; | |
1463 case Magic('C'): | |
1464 regflags |= RF_NOICASE; | |
1465 skipchr_keepstart(); | |
1466 break; | |
1467 case Magic('v'): | |
1468 reg_magic = MAGIC_ALL; | |
1469 skipchr_keepstart(); | |
1470 curchr = -1; | |
1471 break; | |
1472 case Magic('m'): | |
1473 reg_magic = MAGIC_ON; | |
1474 skipchr_keepstart(); | |
1475 curchr = -1; | |
1476 break; | |
1477 case Magic('M'): | |
1478 reg_magic = MAGIC_OFF; | |
1479 skipchr_keepstart(); | |
1480 curchr = -1; | |
1481 break; | |
1482 case Magic('V'): | |
1483 reg_magic = MAGIC_NONE; | |
1484 skipchr_keepstart(); | |
1485 curchr = -1; | |
1486 break; | |
1487 | |
1488 default: | |
1489 if (nfa_regpiece() == FAIL) | |
1490 return FAIL; | |
1491 if (first == FALSE) | |
1492 EMIT(NFA_CONCAT); | |
1493 else | |
1494 first = FALSE; | |
1495 break; | |
1496 } | |
1497 } | |
1498 | |
1499 return OK; | |
1500 } | |
1501 | |
1502 /* | |
1503 * Parse a branch, one or more concats, separated by "\&". It matches the | |
1504 * last concat, but only if all the preceding concats also match at the same | |
1505 * position. Examples: | |
1506 * "foobeep\&..." matches "foo" in "foobeep". | |
1507 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" | |
1508 * | |
1509 * branch ::= concat | |
1510 * or concat \& concat | |
1511 * or concat \& concat \& concat | |
1512 * etc. | |
1513 */ | |
1514 static int | |
1515 nfa_regbranch() | |
1516 { | |
1517 int ch; | |
1518 int *old_post_ptr; | |
1519 | |
1520 old_post_ptr = post_ptr; | |
1521 | |
1522 /* First branch, possibly the only one */ | |
1523 if (nfa_regconcat() == FAIL) | |
1524 return FAIL; | |
1525 | |
1526 ch = peekchr(); | |
1527 /* Try next concats */ | |
1528 while (ch == Magic('&')) | |
1529 { | |
1530 skipchr(); | |
1531 EMIT(NFA_NOPEN); | |
1532 EMIT(NFA_PREV_ATOM_NO_WIDTH); | |
1533 old_post_ptr = post_ptr; | |
1534 if (nfa_regconcat() == FAIL) | |
1535 return FAIL; | |
1536 /* if concat is empty, skip a input char. But do emit a node */ | |
1537 if (old_post_ptr == post_ptr) | |
1538 EMIT(NFA_SKIP_CHAR); | |
1539 EMIT(NFA_CONCAT); | |
1540 ch = peekchr(); | |
1541 } | |
1542 | |
1543 /* Even if a branch is empty, emit one node for it */ | |
1544 if (old_post_ptr == post_ptr) | |
1545 EMIT(NFA_SKIP_CHAR); | |
1546 | |
1547 return OK; | |
1548 } | |
1549 | |
1550 /* | |
1551 * Parse a pattern, one or more branches, separated by "\|". It matches | |
1552 * anything that matches one of the branches. Example: "foo\|beep" matches | |
1553 * "foo" and matches "beep". If more than one branch matches, the first one | |
1554 * is used. | |
1555 * | |
1556 * pattern ::= branch | |
1557 * or branch \| branch | |
1558 * or branch \| branch \| branch | |
1559 * etc. | |
1560 */ | |
1561 static int | |
1562 nfa_reg(paren) | |
1563 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ | |
1564 { | |
1565 int parno = 0; | |
1566 | |
1567 #ifdef FEAT_SYN_HL | |
1568 #endif | |
1569 if (paren == REG_PAREN) | |
1570 { | |
1571 if (regnpar >= NSUBEXP) /* Too many `(' */ | |
1572 { | |
1573 syntax_error = TRUE; | |
1574 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); | |
1575 } | |
1576 parno = regnpar++; | |
1577 } | |
1578 | |
1579 if (nfa_regbranch() == FAIL) | |
1580 return FAIL; /* cascaded error */ | |
1581 | |
1582 while (peekchr() == Magic('|')) | |
1583 { | |
1584 skipchr(); | |
1585 if (nfa_regbranch() == FAIL) | |
1586 return FAIL; /* cascaded error */ | |
1587 EMIT(NFA_OR); | |
1588 } | |
1589 | |
1590 /* Check for proper termination. */ | |
1591 if (paren != REG_NOPAREN && getchr() != Magic(')')) | |
1592 { | |
1593 syntax_error = TRUE; | |
1594 if (paren == REG_NPAREN) | |
1595 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); | |
1596 else | |
1597 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); | |
1598 } | |
1599 else if (paren == REG_NOPAREN && peekchr() != NUL) | |
1600 { | |
1601 syntax_error = TRUE; | |
1602 if (peekchr() == Magic(')')) | |
1603 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); | |
1604 else | |
1605 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); | |
1606 } | |
1607 /* | |
1608 * Here we set the flag allowing back references to this set of | |
1609 * parentheses. | |
1610 */ | |
1611 if (paren == REG_PAREN) | |
1612 { | |
1613 had_endbrace[parno] = TRUE; /* have seen the close paren */ | |
1614 EMIT(NFA_MOPEN + parno); | |
1615 } | |
1616 | |
1617 return OK; | |
1618 } | |
1619 | |
1620 typedef struct | |
1621 { | |
1622 char_u *start[NSUBEXP]; | |
1623 char_u *end[NSUBEXP]; | |
1624 lpos_T startpos[NSUBEXP]; | |
1625 lpos_T endpos[NSUBEXP]; | |
1626 } regsub_T; | |
1627 | |
1628 static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m)); | |
1629 | |
1630 #ifdef DEBUG | |
1631 static char_u code[50]; | |
1632 | |
1633 static void | |
1634 nfa_set_code(c) | |
1635 int c; | |
1636 { | |
1637 int addnl = FALSE; | |
1638 | |
1639 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) | |
1640 { | |
1641 addnl = TRUE; | |
1642 c -= ADD_NL; | |
1643 } | |
1644 | |
1645 STRCPY(code, ""); | |
1646 switch (c) | |
1647 { | |
1648 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; | |
1649 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; | |
1650 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; | |
1651 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; | |
1652 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; | |
1653 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; | |
1654 | |
1655 case NFA_PREV_ATOM_NO_WIDTH: | |
1656 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; | |
1657 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break; | |
1658 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break; | |
1659 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; | |
1660 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; | |
1661 | |
1662 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; | |
1663 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; | |
1664 | |
1665 case NFA_MOPEN + 0: | |
1666 case NFA_MOPEN + 1: | |
1667 case NFA_MOPEN + 2: | |
1668 case NFA_MOPEN + 3: | |
1669 case NFA_MOPEN + 4: | |
1670 case NFA_MOPEN + 5: | |
1671 case NFA_MOPEN + 6: | |
1672 case NFA_MOPEN + 7: | |
1673 case NFA_MOPEN + 8: | |
1674 case NFA_MOPEN + 9: | |
1675 STRCPY(code, "NFA_MOPEN(x)"); | |
1676 code[10] = c - NFA_MOPEN + '0'; | |
1677 break; | |
1678 case NFA_MCLOSE + 0: | |
1679 case NFA_MCLOSE + 1: | |
1680 case NFA_MCLOSE + 2: | |
1681 case NFA_MCLOSE + 3: | |
1682 case NFA_MCLOSE + 4: | |
1683 case NFA_MCLOSE + 5: | |
1684 case NFA_MCLOSE + 6: | |
1685 case NFA_MCLOSE + 7: | |
1686 case NFA_MCLOSE + 8: | |
1687 case NFA_MCLOSE + 9: | |
1688 STRCPY(code, "NFA_MCLOSE(x)"); | |
1689 code[11] = c - NFA_MCLOSE + '0'; | |
1690 break; | |
1691 case NFA_EOL: STRCPY(code, "NFA_EOL "); break; | |
1692 case NFA_BOL: STRCPY(code, "NFA_BOL "); break; | |
1693 case NFA_EOW: STRCPY(code, "NFA_EOW "); break; | |
1694 case NFA_BOW: STRCPY(code, "NFA_BOW "); break; | |
1695 case NFA_STAR: STRCPY(code, "NFA_STAR "); break; | |
1696 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break; | |
1697 case NFA_NOT: STRCPY(code, "NFA_NOT "); break; | |
1698 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; | |
1699 case NFA_OR: STRCPY(code, "NFA_OR"); break; | |
1700 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; | |
1701 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; | |
1702 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break; | |
1703 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; | |
1704 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; | |
1705 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; | |
1706 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; | |
1707 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; | |
1708 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; | |
1709 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; | |
1710 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; | |
1711 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; | |
1712 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; | |
1713 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; | |
1714 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; | |
1715 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; | |
1716 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; | |
1717 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; | |
1718 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; | |
1719 | |
1720 case NFA_ANY: STRCPY(code, "NFA_ANY"); break; | |
1721 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; | |
1722 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; | |
1723 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; | |
1724 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; | |
1725 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; | |
1726 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; | |
1727 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; | |
1728 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; | |
1729 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; | |
1730 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; | |
1731 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; | |
1732 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; | |
1733 case NFA_HEX: STRCPY(code, "NFA_HEX"); break; | |
1734 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; | |
1735 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; | |
1736 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; | |
1737 case NFA_WORD: STRCPY(code, "NFA_WORD"); break; | |
1738 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; | |
1739 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; | |
1740 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; | |
1741 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; | |
1742 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; | |
1743 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; | |
1744 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; | |
1745 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; | |
1746 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; | |
1747 | |
1748 default: | |
1749 STRCPY(code, "CHAR(x)"); | |
1750 code[5] = c; | |
1751 } | |
1752 | |
1753 if (addnl == TRUE) | |
1754 STRCAT(code, " + NEWLINE "); | |
1755 | |
1756 } | |
1757 | |
1758 #ifdef ENABLE_LOG | |
1759 static FILE *log_fd; | |
1760 | |
1761 /* | |
1762 * Print the postfix notation of the current regexp. | |
1763 */ | |
1764 static void | |
1765 nfa_postfix_dump(expr, retval) | |
1766 char_u *expr; | |
1767 int retval; | |
1768 { | |
1769 int *p; | |
1770 FILE *f; | |
1771 | |
1772 f = fopen("LOG.log", "a"); | |
1773 if (f != NULL) | |
1774 { | |
1775 fprintf(f, "\n-------------------------\n"); | |
1776 if (retval == FAIL) | |
1777 fprintf(f, ">>> NFA engine failed ... \n"); | |
1778 else if (retval == OK) | |
1779 fprintf(f, ">>> NFA engine succeeded !\n"); | |
1780 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); | |
4464 | 1781 for (p = post_start; *p && p < post_end; p++) |
4444 | 1782 { |
1783 nfa_set_code(*p); | |
1784 fprintf(f, "%s, ", code); | |
1785 } | |
1786 fprintf(f, "\"\nPostfix notation (int): "); | |
4464 | 1787 for (p = post_start; *p && p < post_end; p++) |
4444 | 1788 fprintf(f, "%d ", *p); |
1789 fprintf(f, "\n\n"); | |
1790 fclose(f); | |
1791 } | |
1792 } | |
1793 | |
1794 /* | |
1795 * Print the NFA starting with a root node "state". | |
1796 */ | |
1797 static void | |
1798 nfa_print_state(debugf, state, ident) | |
1799 FILE *debugf; | |
1800 nfa_state_T *state; | |
1801 int ident; | |
1802 { | |
1803 int i; | |
1804 | |
1805 if (state == NULL) | |
1806 return; | |
1807 | |
1808 fprintf(debugf, "(%2d)", abs(state->id)); | |
1809 for (i = 0; i < ident; i++) | |
1810 fprintf(debugf, "%c", ' '); | |
1811 | |
1812 nfa_set_code(state->c); | |
1813 fprintf(debugf, "%s %s (%d) (id=%d)\n", | |
1814 state->negated ? "NOT" : "", code, state->c, abs(state->id)); | |
1815 if (state->id < 0) | |
1816 return; | |
1817 | |
1818 state->id = abs(state->id) * -1; | |
1819 nfa_print_state(debugf, state->out, ident + 4); | |
1820 nfa_print_state(debugf, state->out1, ident + 4); | |
1821 } | |
1822 | |
1823 /* | |
1824 * Print the NFA state machine. | |
1825 */ | |
1826 static void | |
1827 nfa_dump(prog) | |
1828 nfa_regprog_T *prog; | |
1829 { | |
1830 FILE *debugf = fopen("LOG.log", "a"); | |
1831 | |
1832 if (debugf != NULL) | |
1833 { | |
1834 nfa_print_state(debugf, prog->start, 0); | |
1835 fclose(debugf); | |
1836 } | |
1837 } | |
1838 #endif /* ENABLE_LOG */ | |
1839 #endif /* DEBUG */ | |
1840 | |
1841 /* | |
1842 * Parse r.e. @expr and convert it into postfix form. | |
1843 * Return the postfix string on success, NULL otherwise. | |
1844 */ | |
1845 static int * | |
1846 re2post() | |
1847 { | |
1848 if (nfa_reg(REG_NOPAREN) == FAIL) | |
1849 return NULL; | |
1850 EMIT(NFA_MOPEN); | |
1851 return post_start; | |
1852 } | |
1853 | |
1854 /* NB. Some of the code below is inspired by Russ's. */ | |
1855 | |
1856 /* | |
1857 * Represents an NFA state plus zero or one or two arrows exiting. | |
1858 * if c == MATCH, no arrows out; matching state. | |
1859 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). | |
1860 * If c < 256, labeled arrow with character c to out. | |
1861 */ | |
1862 | |
1863 static nfa_state_T *state_ptr; /* points to nfa_prog->state */ | |
1864 | |
1865 /* | |
1866 * Allocate and initialize nfa_state_T. | |
1867 */ | |
1868 static nfa_state_T * | |
1869 new_state(c, out, out1) | |
1870 int c; | |
1871 nfa_state_T *out; | |
1872 nfa_state_T *out1; | |
1873 { | |
1874 nfa_state_T *s; | |
1875 | |
1876 if (istate >= nstate) | |
1877 return NULL; | |
1878 | |
1879 s = &state_ptr[istate++]; | |
1880 | |
1881 s->c = c; | |
1882 s->out = out; | |
1883 s->out1 = out1; | |
1884 | |
1885 s->id = istate; | |
1886 s->lastlist = 0; | |
1887 s->lastthread = NULL; | |
1888 s->visits = 0; | |
1889 s->negated = FALSE; | |
1890 | |
1891 return s; | |
1892 } | |
1893 | |
1894 /* | |
1895 * A partially built NFA without the matching state filled in. | |
1896 * Frag_T.start points at the start state. | |
1897 * Frag_T.out is a list of places that need to be set to the | |
1898 * next state for this fragment. | |
1899 */ | |
1900 typedef union Ptrlist Ptrlist; | |
1901 struct Frag | |
1902 { | |
1903 nfa_state_T *start; | |
1904 Ptrlist *out; | |
1905 }; | |
1906 typedef struct Frag Frag_T; | |
1907 | |
1908 static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); | |
1909 static Ptrlist *list1 __ARGS((nfa_state_T **outp)); | |
1910 static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); | |
1911 static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); | |
1912 static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); | |
1913 static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); | |
1914 | |
1915 /* | |
4456 | 1916 * Initialize a Frag_T struct and return it. |
4444 | 1917 */ |
1918 static Frag_T | |
1919 frag(start, out) | |
1920 nfa_state_T *start; | |
1921 Ptrlist *out; | |
1922 { | |
4456 | 1923 Frag_T n; |
1924 | |
1925 n.start = start; | |
1926 n.out = out; | |
4444 | 1927 return n; |
1928 } | |
1929 | |
1930 /* | |
1931 * Since the out pointers in the list are always | |
1932 * uninitialized, we use the pointers themselves | |
1933 * as storage for the Ptrlists. | |
1934 */ | |
1935 union Ptrlist | |
1936 { | |
1937 Ptrlist *next; | |
1938 nfa_state_T *s; | |
1939 }; | |
1940 | |
1941 /* | |
1942 * Create singleton list containing just outp. | |
1943 */ | |
1944 static Ptrlist * | |
1945 list1(outp) | |
1946 nfa_state_T **outp; | |
1947 { | |
1948 Ptrlist *l; | |
1949 | |
1950 l = (Ptrlist *)outp; | |
1951 l->next = NULL; | |
1952 return l; | |
1953 } | |
1954 | |
1955 /* | |
1956 * Patch the list of states at out to point to start. | |
1957 */ | |
1958 static void | |
1959 patch(l, s) | |
1960 Ptrlist *l; | |
1961 nfa_state_T *s; | |
1962 { | |
1963 Ptrlist *next; | |
1964 | |
1965 for (; l; l = next) | |
1966 { | |
1967 next = l->next; | |
1968 l->s = s; | |
1969 } | |
1970 } | |
1971 | |
1972 | |
1973 /* | |
1974 * Join the two lists l1 and l2, returning the combination. | |
1975 */ | |
1976 static Ptrlist * | |
1977 append(l1, l2) | |
1978 Ptrlist *l1; | |
1979 Ptrlist *l2; | |
1980 { | |
1981 Ptrlist *oldl1; | |
1982 | |
1983 oldl1 = l1; | |
1984 while (l1->next) | |
1985 l1 = l1->next; | |
1986 l1->next = l2; | |
1987 return oldl1; | |
1988 } | |
1989 | |
1990 /* | |
1991 * Stack used for transforming postfix form into NFA. | |
1992 */ | |
1993 static Frag_T empty; | |
1994 | |
1995 static void | |
1996 st_error(postfix, end, p) | |
1997 int *postfix; | |
1998 int *end; | |
1999 int *p; | |
2000 { | |
2001 FILE *df; | |
2002 int *p2; | |
2003 | |
2004 df = fopen("stack.err", "a"); | |
2005 if (df) | |
2006 { | |
2007 fprintf(df, "Error popping the stack!\n"); | |
2008 #ifdef DEBUG | |
2009 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); | |
2010 #endif | |
2011 fprintf(df, "Postfix form is: "); | |
2012 #ifdef DEBUG | |
2013 for (p2 = postfix; p2 < end; p2++) | |
2014 { | |
2015 nfa_set_code(*p2); | |
2016 fprintf(df, "%s, ", code); | |
2017 } | |
2018 nfa_set_code(*p); | |
2019 fprintf(df, "\nCurrent position is: "); | |
2020 for (p2 = postfix; p2 <= p; p2 ++) | |
2021 { | |
2022 nfa_set_code(*p2); | |
2023 fprintf(df, "%s, ", code); | |
2024 } | |
2025 #else | |
2026 for (p2 = postfix; p2 < end; p2++) | |
2027 { | |
2028 fprintf(df, "%d, ", *p2); | |
2029 } | |
2030 fprintf(df, "\nCurrent position is: "); | |
2031 for (p2 = postfix; p2 <= p; p2 ++) | |
2032 { | |
2033 fprintf(df, "%d, ", *p2); | |
2034 } | |
2035 #endif | |
2036 fprintf(df, "\n--------------------------\n"); | |
2037 fclose(df); | |
2038 } | |
2039 EMSG(_("E874: (NFA) Could not pop the stack !")); | |
2040 } | |
2041 | |
2042 /* | |
2043 * Push an item onto the stack. | |
2044 */ | |
2045 static void | |
2046 st_push(s, p, stack_end) | |
2047 Frag_T s; | |
2048 Frag_T **p; | |
2049 Frag_T *stack_end; | |
2050 { | |
2051 Frag_T *stackp = *p; | |
2052 | |
2053 if (stackp >= stack_end) | |
2054 return; | |
2055 *stackp = s; | |
2056 *p = *p + 1; | |
2057 } | |
2058 | |
2059 /* | |
2060 * Pop an item from the stack. | |
2061 */ | |
2062 static Frag_T | |
2063 st_pop(p, stack) | |
2064 Frag_T **p; | |
2065 Frag_T *stack; | |
2066 { | |
2067 Frag_T *stackp; | |
2068 | |
2069 *p = *p - 1; | |
2070 stackp = *p; | |
2071 if (stackp < stack) | |
2072 return empty; | |
2073 return **p; | |
2074 } | |
2075 | |
2076 /* | |
2077 * Convert a postfix form into its equivalent NFA. | |
2078 * Return the NFA start state on success, NULL otherwise. | |
2079 */ | |
2080 static nfa_state_T * | |
2081 post2nfa(postfix, end, nfa_calc_size) | |
2082 int *postfix; | |
2083 int *end; | |
2084 int nfa_calc_size; | |
2085 { | |
2086 int *p; | |
2087 int mopen; | |
2088 int mclose; | |
2089 Frag_T *stack = NULL; | |
2090 Frag_T *stackp = NULL; | |
2091 Frag_T *stack_end = NULL; | |
2092 Frag_T e1; | |
2093 Frag_T e2; | |
2094 Frag_T e; | |
2095 nfa_state_T *s; | |
2096 nfa_state_T *s1; | |
2097 nfa_state_T *matchstate; | |
4484 | 2098 nfa_state_T *ret = NULL; |
4444 | 2099 |
2100 if (postfix == NULL) | |
2101 return NULL; | |
2102 | |
4456 | 2103 #define PUSH(s) st_push((s), &stackp, stack_end) |
4444 | 2104 #define POP() st_pop(&stackp, stack); \ |
2105 if (stackp < stack) \ | |
2106 { \ | |
2107 st_error(postfix, end, p); \ | |
2108 return NULL; \ | |
2109 } | |
2110 | |
2111 if (nfa_calc_size == FALSE) | |
2112 { | |
2113 /* Allocate space for the stack. Max states on the stack : nstate */ | |
4462 | 2114 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
4444 | 2115 stackp = stack; |
4462 | 2116 stack_end = stack + (nstate + 1); |
4444 | 2117 } |
2118 | |
2119 for (p = postfix; p < end; ++p) | |
2120 { | |
2121 switch (*p) | |
2122 { | |
2123 case NFA_CONCAT: | |
2124 /* Catenation. | |
2125 * Pay attention: this operator does not exist | |
2126 * in the r.e. itself (it is implicit, really). | |
2127 * It is added when r.e. is translated to postfix | |
2128 * form in re2post(). | |
2129 * | |
2130 * No new state added here. */ | |
2131 if (nfa_calc_size == TRUE) | |
2132 { | |
4458 | 2133 /* nstate += 0; */ |
4444 | 2134 break; |
2135 } | |
2136 e2 = POP(); | |
2137 e1 = POP(); | |
2138 patch(e1.out, e2.start); | |
2139 PUSH(frag(e1.start, e2.out)); | |
2140 break; | |
2141 | |
2142 case NFA_NOT: | |
2143 /* Negation of a character */ | |
2144 if (nfa_calc_size == TRUE) | |
2145 { | |
4458 | 2146 /* nstate += 0; */ |
4444 | 2147 break; |
2148 } | |
2149 e1 = POP(); | |
2150 e1.start->negated = TRUE; | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2151 if (e1.start->c == NFA_COMPOSING) |
4444 | 2152 e1.start->out1->negated = TRUE; |
2153 PUSH(e1); | |
2154 break; | |
2155 | |
2156 case NFA_OR: | |
2157 /* Alternation */ | |
2158 if (nfa_calc_size == TRUE) | |
2159 { | |
4458 | 2160 nstate++; |
4444 | 2161 break; |
2162 } | |
2163 e2 = POP(); | |
2164 e1 = POP(); | |
2165 s = new_state(NFA_SPLIT, e1.start, e2.start); | |
2166 if (s == NULL) | |
4484 | 2167 goto theend; |
4444 | 2168 PUSH(frag(s, append(e1.out, e2.out))); |
2169 break; | |
2170 | |
2171 case NFA_STAR: | |
2172 /* Zero or more */ | |
2173 if (nfa_calc_size == TRUE) | |
2174 { | |
4458 | 2175 nstate++; |
4444 | 2176 break; |
2177 } | |
2178 e = POP(); | |
2179 s = new_state(NFA_SPLIT, e.start, NULL); | |
2180 if (s == NULL) | |
4484 | 2181 goto theend; |
4444 | 2182 patch(e.out, s); |
2183 PUSH(frag(s, list1(&s->out1))); | |
2184 break; | |
2185 | |
2186 case NFA_QUEST: | |
2187 /* one or zero atoms=> greedy match */ | |
2188 if (nfa_calc_size == TRUE) | |
2189 { | |
4458 | 2190 nstate++; |
4444 | 2191 break; |
2192 } | |
2193 e = POP(); | |
2194 s = new_state(NFA_SPLIT, e.start, NULL); | |
2195 if (s == NULL) | |
4484 | 2196 goto theend; |
4444 | 2197 PUSH(frag(s, append(e.out, list1(&s->out1)))); |
2198 break; | |
2199 | |
2200 case NFA_QUEST_NONGREEDY: | |
2201 /* zero or one atoms => non-greedy match */ | |
2202 if (nfa_calc_size == TRUE) | |
2203 { | |
4458 | 2204 nstate++; |
4444 | 2205 break; |
2206 } | |
2207 e = POP(); | |
2208 s = new_state(NFA_SPLIT, NULL, e.start); | |
2209 if (s == NULL) | |
4484 | 2210 goto theend; |
4444 | 2211 PUSH(frag(s, append(e.out, list1(&s->out)))); |
2212 break; | |
2213 | |
2214 case NFA_PLUS: | |
2215 /* One or more */ | |
2216 if (nfa_calc_size == TRUE) | |
2217 { | |
4458 | 2218 nstate++; |
4444 | 2219 break; |
2220 } | |
2221 e = POP(); | |
2222 s = new_state(NFA_SPLIT, e.start, NULL); | |
2223 if (s == NULL) | |
4484 | 2224 goto theend; |
4444 | 2225 patch(e.out, s); |
2226 PUSH(frag(e.start, list1(&s->out1))); | |
2227 break; | |
2228 | |
2229 case NFA_SKIP_CHAR: | |
2230 /* Symbol of 0-length, Used in a repetition | |
2231 * with max/min count of 0 */ | |
2232 if (nfa_calc_size == TRUE) | |
2233 { | |
4458 | 2234 nstate++; |
4444 | 2235 break; |
2236 } | |
2237 s = new_state(NFA_SKIP_CHAR, NULL, NULL); | |
2238 if (s == NULL) | |
4484 | 2239 goto theend; |
4444 | 2240 PUSH(frag(s, list1(&s->out))); |
2241 break; | |
2242 | |
2243 case NFA_PREV_ATOM_NO_WIDTH: | |
2244 /* The \@= operator: match the preceding atom with 0 width. | |
2245 * Surrounds the preceding atom with START_INVISIBLE and | |
2246 * END_INVISIBLE, similarly to MOPEN. | |
2247 */ | |
2248 /* TODO: Maybe this drops the speed? */ | |
4484 | 2249 goto theend; |
4444 | 2250 |
2251 if (nfa_calc_size == TRUE) | |
2252 { | |
2253 nstate += 2; | |
2254 break; | |
2255 } | |
2256 e = POP(); | |
2257 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL); | |
2258 if (s1 == NULL) | |
4484 | 2259 goto theend; |
4444 | 2260 patch(e.out, s1); |
2261 | |
2262 s = new_state(NFA_START_INVISIBLE, e.start, s1); | |
2263 if (s == NULL) | |
4484 | 2264 goto theend; |
4444 | 2265 PUSH(frag(s, list1(&s1->out))); |
2266 break; | |
2267 | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2268 case NFA_COMPOSING: /* char with composing char */ |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2269 #if 0 |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2270 /* TODO */ |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2271 if (regflags & RF_ICOMBINE) |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2272 { |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
2273 /* use the base character only */ |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2274 } |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2275 #endif |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2276 /* FALLTHROUGH */ |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2277 |
4444 | 2278 case NFA_MOPEN + 0: /* Submatch */ |
2279 case NFA_MOPEN + 1: | |
2280 case NFA_MOPEN + 2: | |
2281 case NFA_MOPEN + 3: | |
2282 case NFA_MOPEN + 4: | |
2283 case NFA_MOPEN + 5: | |
2284 case NFA_MOPEN + 6: | |
2285 case NFA_MOPEN + 7: | |
2286 case NFA_MOPEN + 8: | |
2287 case NFA_MOPEN + 9: | |
2288 case NFA_NOPEN: /* \%( "Invisible Submatch" */ | |
2289 if (nfa_calc_size == TRUE) | |
2290 { | |
2291 nstate += 2; | |
2292 break; | |
2293 } | |
2294 | |
2295 mopen = *p; | |
2296 switch (*p) | |
2297 { | |
2298 case NFA_NOPEN: | |
2299 mclose = NFA_NCLOSE; | |
2300 break; | |
2301 case NFA_COMPOSING: | |
2302 mclose = NFA_END_COMPOSING; | |
2303 break; | |
2304 default: | |
2305 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */ | |
2306 mclose = *p + NSUBEXP; | |
2307 break; | |
2308 } | |
2309 | |
2310 /* Allow "NFA_MOPEN" as a valid postfix representation for | |
2311 * the empty regexp "". In this case, the NFA will be | |
2312 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows | |
2313 * empty groups of parenthesis, and empty mbyte chars */ | |
2314 if (stackp == stack) | |
2315 { | |
2316 s = new_state(mopen, NULL, NULL); | |
2317 if (s == NULL) | |
4484 | 2318 goto theend; |
4444 | 2319 s1 = new_state(mclose, NULL, NULL); |
2320 if (s1 == NULL) | |
4484 | 2321 goto theend; |
4444 | 2322 patch(list1(&s->out), s1); |
2323 PUSH(frag(s, list1(&s1->out))); | |
2324 break; | |
2325 } | |
2326 | |
2327 /* At least one node was emitted before NFA_MOPEN, so | |
2328 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ | |
2329 e = POP(); | |
2330 s = new_state(mopen, e.start, NULL); /* `(' */ | |
2331 if (s == NULL) | |
4484 | 2332 goto theend; |
4444 | 2333 |
2334 s1 = new_state(mclose, NULL, NULL); /* `)' */ | |
2335 if (s1 == NULL) | |
4484 | 2336 goto theend; |
4444 | 2337 patch(e.out, s1); |
2338 | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2339 if (mopen == NFA_COMPOSING) |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2340 /* COMPOSING->out1 = END_COMPOSING */ |
4444 | 2341 patch(list1(&s->out1), s1); |
2342 | |
2343 PUSH(frag(s, list1(&s1->out))); | |
2344 break; | |
2345 | |
2346 case NFA_ZSTART: | |
2347 case NFA_ZEND: | |
2348 default: | |
2349 /* Operands */ | |
2350 if (nfa_calc_size == TRUE) | |
2351 { | |
4458 | 2352 nstate++; |
4444 | 2353 break; |
2354 } | |
2355 s = new_state(*p, NULL, NULL); | |
2356 if (s == NULL) | |
4484 | 2357 goto theend; |
4444 | 2358 PUSH(frag(s, list1(&s->out))); |
2359 break; | |
2360 | |
2361 } /* switch(*p) */ | |
2362 | |
2363 } /* for(p = postfix; *p; ++p) */ | |
2364 | |
2365 if (nfa_calc_size == TRUE) | |
2366 { | |
4458 | 2367 nstate++; |
4484 | 2368 goto theend; /* Return value when counting size is ignored anyway */ |
4444 | 2369 } |
2370 | |
2371 e = POP(); | |
2372 if (stackp != stack) | |
2373 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); | |
2374 | |
2375 if (istate >= nstate) | |
2376 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); | |
2377 | |
2378 matchstate = &state_ptr[istate++]; /* the match state */ | |
2379 matchstate->c = NFA_MATCH; | |
2380 matchstate->out = matchstate->out1 = NULL; | |
2381 | |
2382 patch(e.out, matchstate); | |
4484 | 2383 ret = e.start; |
2384 | |
2385 theend: | |
2386 vim_free(stack); | |
2387 return ret; | |
4444 | 2388 |
2389 #undef POP1 | |
2390 #undef PUSH1 | |
2391 #undef POP2 | |
2392 #undef PUSH2 | |
2393 #undef POP | |
2394 #undef PUSH | |
2395 } | |
2396 | |
2397 /**************************************************************** | |
2398 * NFA execution code. | |
2399 ****************************************************************/ | |
2400 | |
2401 /* thread_T contains runtime information of a NFA state */ | |
2402 struct thread | |
2403 { | |
2404 nfa_state_T *state; | |
2405 regsub_T sub; /* submatch info */ | |
2406 }; | |
2407 | |
2408 typedef struct | |
2409 { | |
2410 thread_T *t; | |
2411 int n; | |
2412 } List; | |
2413 | |
2414 static void addstate __ARGS((List *l, nfa_state_T *state, regsub_T *m, int off, int lid, int *match)); | |
2415 | |
2416 static void | |
2417 addstate(l, state, m, off, lid, match) | |
2418 List *l; /* runtime state list */ | |
2419 nfa_state_T *state; /* state to update */ | |
2420 regsub_T *m; /* pointers to subexpressions */ | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2421 int off; /* byte offset, when -1 go to next line */ |
4444 | 2422 int lid; |
2423 int *match; /* found match? */ | |
2424 { | |
2425 regsub_T save; | |
2426 int subidx = 0; | |
2427 | |
2428 if (l == NULL || state == NULL) | |
2429 return; | |
2430 | |
2431 switch (state->c) | |
2432 { | |
2433 case NFA_SPLIT: | |
2434 case NFA_NOT: | |
2435 case NFA_NOPEN: | |
2436 case NFA_NCLOSE: | |
2437 case NFA_MCLOSE: | |
2438 case NFA_MCLOSE + 1: | |
2439 case NFA_MCLOSE + 2: | |
2440 case NFA_MCLOSE + 3: | |
2441 case NFA_MCLOSE + 4: | |
2442 case NFA_MCLOSE + 5: | |
2443 case NFA_MCLOSE + 6: | |
2444 case NFA_MCLOSE + 7: | |
2445 case NFA_MCLOSE + 8: | |
2446 case NFA_MCLOSE + 9: | |
2447 /* Do not remember these nodes in list "thislist" or "nextlist" */ | |
2448 break; | |
2449 | |
2450 default: | |
2451 if (state->lastlist == lid) | |
2452 { | |
2453 if (++state->visits > 2) | |
2454 return; | |
2455 } | |
2456 else | |
2457 { | |
2458 /* add the state to the list */ | |
2459 state->lastlist = lid; | |
2460 state->lastthread = &l->t[l->n++]; | |
2461 state->lastthread->state = state; | |
2462 state->lastthread->sub = *m; | |
2463 } | |
2464 } | |
2465 | |
2466 #ifdef ENABLE_LOG | |
2467 nfa_set_code(state->c); | |
2468 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n", | |
2469 abs(state->id), code, state->c); | |
2470 #endif | |
2471 switch (state->c) | |
2472 { | |
2473 case NFA_MATCH: | |
2474 *match = TRUE; | |
2475 break; | |
2476 | |
2477 case NFA_SPLIT: | |
2478 addstate(l, state->out, m, off, lid, match); | |
2479 addstate(l, state->out1, m, off, lid, match); | |
2480 break; | |
2481 | |
2482 case NFA_SKIP_CHAR: | |
2483 addstate(l, state->out, m, off, lid, match); | |
2484 break; | |
2485 | |
2486 #if 0 | |
2487 case NFA_END_NEG_RANGE: | |
2488 /* Nothing to handle here. nfa_regmatch() will take care of it */ | |
2489 break; | |
2490 | |
2491 case NFA_NOT: | |
2492 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !")); | |
2493 #ifdef ENABLE_LOG | |
2494 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n"); | |
2495 #endif | |
2496 break; | |
2497 | |
2498 case NFA_COMPOSING: | |
2499 /* nfa_regmatch() will match all the bytes of this composing char. */ | |
2500 break; | |
2501 #endif | |
2502 | |
2503 case NFA_NOPEN: | |
2504 case NFA_NCLOSE: | |
2505 addstate(l, state->out, m, off, lid, match); | |
2506 break; | |
2507 | |
2508 /* If this state is reached, then a recursive call of nfa_regmatch() | |
2509 * succeeded. the next call saves the found submatches in the | |
2510 * first state after the "invisible" branch. */ | |
2511 #if 0 | |
2512 case NFA_END_INVISIBLE: | |
2513 break; | |
2514 #endif | |
2515 | |
2516 case NFA_MOPEN + 0: | |
2517 case NFA_MOPEN + 1: | |
2518 case NFA_MOPEN + 2: | |
2519 case NFA_MOPEN + 3: | |
2520 case NFA_MOPEN + 4: | |
2521 case NFA_MOPEN + 5: | |
2522 case NFA_MOPEN + 6: | |
2523 case NFA_MOPEN + 7: | |
2524 case NFA_MOPEN + 8: | |
2525 case NFA_MOPEN + 9: | |
2526 case NFA_ZSTART: | |
2527 subidx = state->c - NFA_MOPEN; | |
2528 if (state->c == NFA_ZSTART) | |
2529 subidx = 0; | |
2530 | |
2531 if (REG_MULTI) | |
2532 { | |
2533 save.startpos[subidx] = m->startpos[subidx]; | |
2534 save.endpos[subidx] = m->endpos[subidx]; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2535 if (off == -1) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2536 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2537 m->startpos[subidx].lnum = reglnum + 1; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2538 m->startpos[subidx].col = 0; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2539 } |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2540 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2541 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2542 m->startpos[subidx].lnum = reglnum; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2543 m->startpos[subidx].col = |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2544 (colnr_T)(reginput - regline + off); |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2545 } |
4444 | 2546 } |
2547 else | |
2548 { | |
2549 save.start[subidx] = m->start[subidx]; | |
2550 save.end[subidx] = m->end[subidx]; | |
2551 m->start[subidx] = reginput + off; | |
2552 } | |
2553 | |
2554 addstate(l, state->out, m, off, lid, match); | |
2555 | |
2556 if (REG_MULTI) | |
2557 { | |
2558 m->startpos[subidx] = save.startpos[subidx]; | |
2559 m->endpos[subidx] = save.endpos[subidx]; | |
2560 } | |
2561 else | |
2562 { | |
2563 m->start[subidx] = save.start[subidx]; | |
2564 m->end[subidx] = save.end[subidx]; | |
2565 } | |
2566 break; | |
2567 | |
2568 case NFA_MCLOSE + 0: | |
2569 if (nfa_has_zend == TRUE) | |
2570 { | |
2571 addstate(l, state->out, m, off, lid, match); | |
2572 break; | |
2573 } | |
2574 case NFA_MCLOSE + 1: | |
2575 case NFA_MCLOSE + 2: | |
2576 case NFA_MCLOSE + 3: | |
2577 case NFA_MCLOSE + 4: | |
2578 case NFA_MCLOSE + 5: | |
2579 case NFA_MCLOSE + 6: | |
2580 case NFA_MCLOSE + 7: | |
2581 case NFA_MCLOSE + 8: | |
2582 case NFA_MCLOSE + 9: | |
2583 case NFA_ZEND: | |
2584 subidx = state->c - NFA_MCLOSE; | |
2585 if (state->c == NFA_ZEND) | |
2586 subidx = 0; | |
2587 | |
2588 if (REG_MULTI) | |
2589 { | |
2590 save.startpos[subidx] = m->startpos[subidx]; | |
2591 save.endpos[subidx] = m->endpos[subidx]; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2592 if (off == -1) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2593 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2594 m->endpos[subidx].lnum = reglnum + 1; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2595 m->endpos[subidx].col = 0; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2596 } |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2597 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2598 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2599 m->endpos[subidx].lnum = reglnum; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2600 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
|
2601 } |
4444 | 2602 } |
2603 else | |
2604 { | |
2605 save.start[subidx] = m->start[subidx]; | |
2606 save.end[subidx] = m->end[subidx]; | |
2607 m->end[subidx] = reginput + off; | |
2608 } | |
2609 | |
2610 addstate(l, state->out, m, off, lid, match); | |
2611 | |
2612 if (REG_MULTI) | |
2613 { | |
2614 m->startpos[subidx] = save.startpos[subidx]; | |
2615 m->endpos[subidx] = save.endpos[subidx]; | |
2616 } | |
2617 else | |
2618 { | |
2619 m->start[subidx] = save.start[subidx]; | |
2620 m->end[subidx] = save.end[subidx]; | |
2621 } | |
2622 break; | |
2623 } | |
2624 } | |
2625 | |
2626 /* | |
2627 * Check character class "class" against current character c. | |
2628 */ | |
2629 static int | |
2630 check_char_class(class, c) | |
2631 int class; | |
2632 int c; | |
2633 { | |
2634 switch (class) | |
2635 { | |
2636 case NFA_CLASS_ALNUM: | |
4464 | 2637 if (c >= 1 && c <= 255 && isalnum(c)) |
4444 | 2638 return OK; |
2639 break; | |
2640 case NFA_CLASS_ALPHA: | |
4464 | 2641 if (c >= 1 && c <= 255 && isalpha(c)) |
4444 | 2642 return OK; |
2643 break; | |
2644 case NFA_CLASS_BLANK: | |
2645 if (c == ' ' || c == '\t') | |
2646 return OK; | |
2647 break; | |
2648 case NFA_CLASS_CNTRL: | |
4464 | 2649 if (c >= 1 && c <= 255 && iscntrl(c)) |
4444 | 2650 return OK; |
2651 break; | |
2652 case NFA_CLASS_DIGIT: | |
2653 if (VIM_ISDIGIT(c)) | |
2654 return OK; | |
2655 break; | |
2656 case NFA_CLASS_GRAPH: | |
4464 | 2657 if (c >= 1 && c <= 255 && isgraph(c)) |
4444 | 2658 return OK; |
2659 break; | |
2660 case NFA_CLASS_LOWER: | |
2661 if (MB_ISLOWER(c)) | |
2662 return OK; | |
2663 break; | |
2664 case NFA_CLASS_PRINT: | |
2665 if (vim_isprintc(c)) | |
2666 return OK; | |
2667 break; | |
2668 case NFA_CLASS_PUNCT: | |
4464 | 2669 if (c >= 1 && c <= 255 && ispunct(c)) |
4444 | 2670 return OK; |
2671 break; | |
2672 case NFA_CLASS_SPACE: | |
2673 if ((c >=9 && c <= 13) || (c == ' ')) | |
2674 return OK; | |
2675 break; | |
2676 case NFA_CLASS_UPPER: | |
2677 if (MB_ISUPPER(c)) | |
2678 return OK; | |
2679 break; | |
2680 case NFA_CLASS_XDIGIT: | |
2681 if (vim_isxdigit(c)) | |
2682 return OK; | |
2683 break; | |
2684 case NFA_CLASS_TAB: | |
2685 if (c == '\t') | |
2686 return OK; | |
2687 break; | |
2688 case NFA_CLASS_RETURN: | |
2689 if (c == '\r') | |
2690 return OK; | |
2691 break; | |
2692 case NFA_CLASS_BACKSPACE: | |
2693 if (c == '\b') | |
2694 return OK; | |
2695 break; | |
2696 case NFA_CLASS_ESCAPE: | |
2697 if (c == '\033') | |
2698 return OK; | |
2699 break; | |
2700 | |
2701 default: | |
2702 /* should not be here :P */ | |
2703 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class ")); | |
2704 } | |
2705 return FAIL; | |
2706 } | |
2707 | |
2708 /* | |
2709 * Set all NFA nodes' list ID equal to -1. | |
2710 */ | |
2711 static void | |
2712 nfa_set_neg_listids(start) | |
2713 nfa_state_T *start; | |
2714 { | |
2715 if (start == NULL) | |
2716 return; | |
2717 if (start->lastlist >= 0) | |
2718 { | |
2719 start->lastlist = -1; | |
2720 nfa_set_neg_listids(start->out); | |
2721 nfa_set_neg_listids(start->out1); | |
2722 } | |
2723 } | |
2724 | |
2725 /* | |
2726 * Set all NFA nodes' list ID equal to 0. | |
2727 */ | |
2728 static void | |
2729 nfa_set_null_listids(start) | |
2730 nfa_state_T *start; | |
2731 { | |
2732 if (start == NULL) | |
2733 return; | |
2734 if (start->lastlist == -1) | |
2735 { | |
2736 start->lastlist = 0; | |
2737 nfa_set_null_listids(start->out); | |
2738 nfa_set_null_listids(start->out1); | |
2739 } | |
2740 } | |
2741 | |
2742 /* | |
2743 * Save list IDs for all NFA states in "list". | |
2744 */ | |
2745 static void | |
2746 nfa_save_listids(start, list) | |
2747 nfa_state_T *start; | |
2748 int *list; | |
2749 { | |
2750 if (start == NULL) | |
2751 return; | |
2752 if (start->lastlist != -1) | |
2753 { | |
2754 list[abs(start->id)] = start->lastlist; | |
2755 start->lastlist = -1; | |
2756 nfa_save_listids(start->out, list); | |
2757 nfa_save_listids(start->out1, list); | |
2758 } | |
2759 } | |
2760 | |
2761 /* | |
2762 * Restore list IDs from "list" to all NFA states. | |
2763 */ | |
2764 static void | |
2765 nfa_restore_listids(start, list) | |
2766 nfa_state_T *start; | |
2767 int *list; | |
2768 { | |
2769 if (start == NULL) | |
2770 return; | |
2771 if (start->lastlist == -1) | |
2772 { | |
2773 start->lastlist = list[abs(start->id)]; | |
2774 nfa_restore_listids(start->out, list); | |
2775 nfa_restore_listids(start->out1, list); | |
2776 } | |
2777 } | |
2778 | |
2779 /* | |
2780 * Main matching routine. | |
2781 * | |
2782 * Run NFA to determine whether it matches reginput. | |
2783 * | |
2784 * Return TRUE if there is a match, FALSE otherwise. | |
2785 * Note: Caller must ensure that: start != NULL. | |
2786 */ | |
2787 static int | |
2788 nfa_regmatch(start, submatch, m) | |
2789 nfa_state_T *start; | |
2790 regsub_T *submatch; | |
2791 regsub_T *m; | |
2792 { | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
2793 int c; |
4444 | 2794 int n; |
2795 int i = 0; | |
2796 int result; | |
2797 int size = 0; | |
2798 int match = FALSE; | |
2799 int flag = 0; | |
2800 int old_reglnum = -1; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2801 int go_to_nextline; |
4444 | 2802 thread_T *t; |
2803 char_u *old_reginput = NULL; | |
2804 char_u *old_regline = NULL; | |
2805 nfa_state_T *sta; | |
2806 nfa_state_T *end; | |
2807 List list[3]; | |
2808 List *listtbl[2][2]; | |
2809 List *ll; | |
2810 int listid = 1; | |
2811 List *thislist; | |
2812 List *nextlist; | |
2813 List *neglist; | |
2814 int *listids = NULL; | |
2815 int j = 0; | |
2816 int len = 0; | |
4460 | 2817 #ifdef NFA_REGEXP_DEBUG_LOG |
2818 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG_NAME, "a"); | |
4444 | 2819 |
2820 if (debug == NULL) | |
2821 { | |
4460 | 2822 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG_NAME); |
4444 | 2823 return FALSE; |
2824 } | |
2825 #endif | |
2826 | |
2827 /* Allocate memory for the lists of nodes */ | |
2828 size = (nstate + 1) * sizeof(thread_T); | |
2829 list[0].t = (thread_T *)lalloc(size, TRUE); | |
2830 list[1].t = (thread_T *)lalloc(size, TRUE); | |
2831 list[2].t = (thread_T *)lalloc(size, TRUE); | |
2832 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) | |
2833 goto theend; | |
2834 vim_memset(list[0].t, 0, size); | |
2835 vim_memset(list[1].t, 0, size); | |
2836 vim_memset(list[2].t, 0, size); | |
2837 | |
2838 #ifdef ENABLE_LOG | |
2839 log_fd = fopen(LOG_NAME, "a"); | |
2840 if (log_fd != NULL) | |
2841 { | |
2842 fprintf(log_fd, "**********************************\n"); | |
2843 nfa_set_code(start->c); | |
2844 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", | |
2845 abs(start->id), code); | |
2846 fprintf(log_fd, "**********************************\n"); | |
2847 } | |
2848 else | |
2849 { | |
2850 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); | |
2851 log_fd = stderr; | |
2852 } | |
2853 #endif | |
2854 | |
2855 thislist = &list[0]; | |
2856 thislist->n = 0; | |
2857 nextlist = &list[1]; | |
2858 nextlist->n = 0; | |
2859 neglist = &list[2]; | |
2860 neglist->n = 0; | |
2861 #ifdef ENABLE_LOG | |
2862 fprintf(log_fd, "(---) STARTSTATE\n"); | |
2863 #endif | |
2864 addstate(thislist, start, m, 0, listid, &match); | |
2865 | |
2866 /* There are two cases when the NFA advances: 1. input char matches the | |
2867 * NFA node and 2. input char does not match the NFA node, but the next | |
2868 * node is NFA_NOT. The following macro calls addstate() according to | |
2869 * these rules. It is used A LOT, so use the "listtbl" table for speed */ | |
2870 listtbl[0][0] = NULL; | |
2871 listtbl[0][1] = neglist; | |
2872 listtbl[1][0] = nextlist; | |
2873 listtbl[1][1] = NULL; | |
2874 #define ADD_POS_NEG_STATE(node) \ | |
2875 ll = listtbl[result ? 1 : 0][node->negated]; \ | |
2876 if (ll != NULL) \ | |
2877 addstate(ll, node->out , &t->sub, n, listid + 1, &match); | |
2878 | |
2879 | |
2880 /* | |
2881 * Run for each character. | |
2882 */ | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2883 for (;;) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2884 { |
4444 | 2885 #ifdef FEAT_MBYTE |
2886 if (has_mbyte) | |
2887 { | |
2888 c = (*mb_ptr2char)(reginput); | |
2889 n = (*mb_ptr2len)(reginput); | |
2890 } | |
2891 else | |
2892 #endif | |
2893 { | |
2894 c = *reginput; | |
2895 n = 1; | |
2896 } | |
2897 if (c == NUL) | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2898 { |
4444 | 2899 n = 0; |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2900 go_to_nextline = FALSE; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2901 } |
4444 | 2902 |
2903 /* swap lists */ | |
2904 thislist = &list[flag]; | |
2905 nextlist = &list[flag ^= 1]; | |
2906 nextlist->n = 0; /* `clear' nextlist */ | |
2907 listtbl[1][0] = nextlist; | |
2908 ++listid; | |
2909 | |
2910 #ifdef ENABLE_LOG | |
2911 fprintf(log_fd, "------------------------------------------\n"); | |
2912 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); | |
2913 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", c, (int)c); | |
2914 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); | |
2915 for (i = 0; i< thislist->n; i++) | |
2916 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); | |
2917 fprintf(log_fd, "\n"); | |
2918 #endif | |
2919 | |
4460 | 2920 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 2921 fprintf(debug, "\n-------------------\n"); |
2922 #endif | |
4480 | 2923 /* |
2924 * If the state lists are empty we can stop. | |
2925 */ | |
2926 if (thislist->n == 0 && neglist->n == 0) | |
2927 break; | |
4444 | 2928 |
2929 /* compute nextlist */ | |
2930 for (i = 0; i < thislist->n || neglist->n > 0; ++i) | |
2931 { | |
2932 if (neglist->n > 0) | |
2933 { | |
2934 t = &neglist->t[0]; | |
4468 | 2935 neglist->n--; |
4444 | 2936 i--; |
2937 } | |
2938 else | |
2939 t = &thislist->t[i]; | |
2940 | |
4460 | 2941 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 2942 nfa_set_code(t->state->c); |
2943 fprintf(debug, "%s, ", code); | |
2944 #endif | |
2945 #ifdef ENABLE_LOG | |
2946 nfa_set_code(t->state->c); | |
2947 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id), | |
2948 code, (int)t->state->c); | |
2949 #endif | |
2950 | |
2951 /* | |
2952 * Handle the possible codes of the current state. | |
2953 * The most important is NFA_MATCH. | |
2954 */ | |
2955 switch (t->state->c) | |
2956 { | |
2957 case NFA_MATCH: | |
2958 match = TRUE; | |
2959 *submatch = t->sub; | |
2960 #ifdef ENABLE_LOG | |
2961 for (j = 0; j < 4; j++) | |
2962 if (REG_MULTI) | |
2963 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d", | |
2964 j, | |
2965 t->sub.startpos[j].col, | |
2966 (int)t->sub.startpos[j].lnum, | |
2967 t->sub.endpos[j].col, | |
2968 (int)t->sub.endpos[j].lnum); | |
2969 else | |
2970 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"", | |
2971 j, | |
2972 (char *)t->sub.start[j], | |
2973 (char *)t->sub.end[j]); | |
2974 fprintf(log_fd, "\n"); | |
2975 #endif | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2976 /* 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
|
2977 * states at this position. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2978 goto nextchar; |
4444 | 2979 |
2980 case NFA_END_INVISIBLE: | |
2981 /* This is only encountered after a NFA_START_INVISIBLE node. | |
2982 * They surround a zero-width group, used with "\@=" and "\&". | |
2983 * If we got here, it means that the current "invisible" group | |
2984 * finished successfully, so return control to the parent | |
2985 * nfa_regmatch(). Submatches are stored in *m, and used in | |
2986 * the parent call. */ | |
2987 if (start->c == NFA_MOPEN + 0) | |
2988 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
2989 &match); | |
2990 else | |
2991 { | |
2992 *m = t->sub; | |
2993 match = TRUE; | |
2994 } | |
2995 break; | |
2996 | |
2997 case NFA_START_INVISIBLE: | |
2998 /* Save global variables, and call nfa_regmatch() to check if | |
2999 * the current concat matches at this position. The concat | |
3000 * ends with the node NFA_END_INVISIBLE */ | |
3001 old_reginput = reginput; | |
3002 old_regline = regline; | |
3003 old_reglnum = reglnum; | |
3004 if (listids == NULL) | |
3005 { | |
3006 listids = (int *) lalloc(sizeof(int) * nstate, TRUE); | |
3007 if (listids == NULL) | |
3008 { | |
3009 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); | |
3010 return 0; | |
3011 } | |
3012 } | |
3013 #ifdef ENABLE_LOG | |
3014 if (log_fd != stderr) | |
3015 fclose(log_fd); | |
3016 log_fd = NULL; | |
3017 #endif | |
3018 /* Have to clear the listid field of the NFA nodes, so that | |
3019 * nfa_regmatch() and addstate() can run properly after | |
3020 * recursion. */ | |
3021 nfa_save_listids(start, listids); | |
3022 nfa_set_null_listids(start); | |
3023 result = nfa_regmatch(t->state->out, submatch, m); | |
3024 nfa_set_neg_listids(start); | |
3025 nfa_restore_listids(start, listids); | |
3026 | |
3027 #ifdef ENABLE_LOG | |
3028 log_fd = fopen(LOG_NAME, "a"); | |
3029 if (log_fd != NULL) | |
3030 { | |
3031 fprintf(log_fd, "****************************\n"); | |
3032 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); | |
3033 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); | |
3034 fprintf(log_fd, "****************************\n"); | |
3035 } | |
3036 else | |
3037 { | |
3038 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); | |
3039 log_fd = stderr; | |
3040 } | |
3041 #endif | |
3042 if (result == TRUE) | |
3043 { | |
3044 /* Restore position in input text */ | |
3045 reginput = old_reginput; | |
3046 regline = old_regline; | |
3047 reglnum = old_reglnum; | |
3048 /* Copy submatch info from the recursive call */ | |
3049 if (REG_MULTI) | |
3050 for (j = 1; j < NSUBEXP; j++) | |
3051 { | |
3052 t->sub.startpos[j] = m->startpos[j]; | |
3053 t->sub.endpos[j] = m->endpos[j]; | |
3054 } | |
3055 else | |
3056 for (j = 1; j < NSUBEXP; j++) | |
3057 { | |
3058 t->sub.start[j] = m->start[j]; | |
3059 t->sub.end[j] = m->end[j]; | |
3060 } | |
3061 /* t->state->out1 is the corresponding END_INVISIBLE node */ | |
3062 addstate(thislist, t->state->out1->out, &t->sub, 0, listid, | |
3063 &match); | |
3064 } | |
3065 else | |
3066 { | |
3067 /* continue with next input char */ | |
3068 reginput = old_reginput; | |
3069 } | |
3070 break; | |
3071 | |
3072 case NFA_BOL: | |
3073 if (reginput == regline) | |
3074 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3075 &match); | |
3076 break; | |
3077 | |
3078 case NFA_EOL: | |
3079 if (c == NUL) | |
3080 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3081 &match); | |
3082 break; | |
3083 | |
3084 case NFA_BOW: | |
3085 { | |
3086 int bow = TRUE; | |
3087 | |
3088 if (c == NUL) | |
3089 bow = FALSE; | |
3090 #ifdef FEAT_MBYTE | |
3091 else if (has_mbyte) | |
3092 { | |
3093 int this_class; | |
3094 | |
3095 /* Get class of current and previous char (if it exists). */ | |
4503 | 3096 this_class = mb_get_class_buf(reginput, reg_buf); |
4444 | 3097 if (this_class <= 1) |
3098 bow = FALSE; | |
3099 else if (reg_prev_class() == this_class) | |
3100 bow = FALSE; | |
3101 } | |
3102 #endif | |
4503 | 3103 else if (!vim_iswordc_buf(c, reg_buf) |
3104 || (reginput > regline | |
3105 && vim_iswordc_buf(reginput[-1], reg_buf))) | |
4444 | 3106 bow = FALSE; |
3107 if (bow) | |
3108 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3109 &match); | |
3110 break; | |
3111 } | |
3112 | |
3113 case NFA_EOW: | |
3114 { | |
3115 int eow = TRUE; | |
3116 | |
3117 if (reginput == regline) | |
3118 eow = FALSE; | |
3119 #ifdef FEAT_MBYTE | |
3120 else if (has_mbyte) | |
3121 { | |
3122 int this_class, prev_class; | |
3123 | |
3124 /* Get class of current and previous char (if it exists). */ | |
4503 | 3125 this_class = mb_get_class_buf(reginput, reg_buf); |
4444 | 3126 prev_class = reg_prev_class(); |
3127 if (this_class == prev_class | |
3128 || prev_class == 0 || prev_class == 1) | |
3129 eow = FALSE; | |
3130 } | |
3131 #endif | |
4503 | 3132 else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
3133 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf))) | |
4444 | 3134 eow = FALSE; |
3135 if (eow) | |
3136 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3137 &match); | |
3138 break; | |
3139 } | |
3140 | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3141 #ifdef FEAT_MBYTE |
4444 | 3142 case NFA_COMPOSING: |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3143 { |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3144 int mc = c; |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3145 |
4444 | 3146 result = OK; |
3147 sta = t->state->out; | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3148 len = 0; |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3149 if (ireg_icombine) |
4444 | 3150 { |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3151 /* If \Z was present, then ignore composing characters. */ |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3152 /* TODO: How about negated? */ |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3153 if (sta->c != c) |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3154 result = FAIL; |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3155 len = n; |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3156 while (sta->c != NFA_END_COMPOSING) |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3157 sta = sta->out; |
4444 | 3158 } |
4529
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3159 else |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3160 while (sta->c != NFA_END_COMPOSING && len < n) |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3161 { |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3162 if (len > 0) |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3163 mc = mb_ptr2char(reginput + len); |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3164 if (mc != sta->c) |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3165 break; |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3166 len += mb_char2len(mc); |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3167 sta = sta->out; |
432a6b8c7d93
updated for version 7.3.1012
Bram Moolenaar <bram@vim.org>
parents:
4527
diff
changeset
|
3168 } |
4444 | 3169 |
3170 /* 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
|
3171 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
|
3172 result = FAIL; |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3173 end = t->state->out1; /* NFA_END_COMPOSING */ |
4444 | 3174 ADD_POS_NEG_STATE(end); |
3175 break; | |
4527
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3176 } |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3177 #endif |
4444 | 3178 |
3179 case NFA_NEWL: | |
3180 if (!reg_line_lbr && REG_MULTI | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3181 && c == NUL && reglnum <= reg_maxline) |
4444 | 3182 { |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3183 go_to_nextline = TRUE; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3184 /* 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
|
3185 * at the start of the next line. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3186 addstate(nextlist, t->state->out, &t->sub, -1, |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3187 listid + 1, &match); |
4444 | 3188 } |
3189 break; | |
3190 | |
3191 case NFA_CLASS_ALNUM: | |
3192 case NFA_CLASS_ALPHA: | |
3193 case NFA_CLASS_BLANK: | |
3194 case NFA_CLASS_CNTRL: | |
3195 case NFA_CLASS_DIGIT: | |
3196 case NFA_CLASS_GRAPH: | |
3197 case NFA_CLASS_LOWER: | |
3198 case NFA_CLASS_PRINT: | |
3199 case NFA_CLASS_PUNCT: | |
3200 case NFA_CLASS_SPACE: | |
3201 case NFA_CLASS_UPPER: | |
3202 case NFA_CLASS_XDIGIT: | |
3203 case NFA_CLASS_TAB: | |
3204 case NFA_CLASS_RETURN: | |
3205 case NFA_CLASS_BACKSPACE: | |
3206 case NFA_CLASS_ESCAPE: | |
3207 result = check_char_class(t->state->c, c); | |
3208 ADD_POS_NEG_STATE(t->state); | |
3209 break; | |
3210 | |
3211 case NFA_END_NEG_RANGE: | |
3212 /* This follows a series of negated nodes, like: | |
3213 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */ | |
3214 if (c > 0) | |
3215 addstate(nextlist, t->state->out, &t->sub, n, listid + 1, | |
3216 &match); | |
3217 break; | |
3218 | |
3219 case NFA_ANY: | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3220 /* Any char except '\0', (end of input) does not match. */ |
4444 | 3221 if (c > 0) |
3222 addstate(nextlist, t->state->out, &t->sub, n, listid + 1, | |
3223 &match); | |
3224 break; | |
3225 | |
3226 /* | |
3227 * Character classes like \a for alpha, \d for digit etc. | |
3228 */ | |
3229 case NFA_IDENT: /* \i */ | |
3230 result = vim_isIDc(c); | |
3231 ADD_POS_NEG_STATE(t->state); | |
3232 break; | |
3233 | |
3234 case NFA_SIDENT: /* \I */ | |
3235 result = !VIM_ISDIGIT(c) && vim_isIDc(c); | |
3236 ADD_POS_NEG_STATE(t->state); | |
3237 break; | |
3238 | |
3239 case NFA_KWORD: /* \k */ | |
4503 | 3240 result = vim_iswordp_buf(reginput, reg_buf); |
4444 | 3241 ADD_POS_NEG_STATE(t->state); |
3242 break; | |
3243 | |
3244 case NFA_SKWORD: /* \K */ | |
4503 | 3245 result = !VIM_ISDIGIT(c) && vim_iswordp_buf(reginput, reg_buf); |
4444 | 3246 ADD_POS_NEG_STATE(t->state); |
3247 break; | |
3248 | |
3249 case NFA_FNAME: /* \f */ | |
3250 result = vim_isfilec(c); | |
3251 ADD_POS_NEG_STATE(t->state); | |
3252 break; | |
3253 | |
3254 case NFA_SFNAME: /* \F */ | |
3255 result = !VIM_ISDIGIT(c) && vim_isfilec(c); | |
3256 ADD_POS_NEG_STATE(t->state); | |
3257 break; | |
3258 | |
3259 case NFA_PRINT: /* \p */ | |
4470 | 3260 result = ptr2cells(reginput) == 1; |
4444 | 3261 ADD_POS_NEG_STATE(t->state); |
3262 break; | |
3263 | |
3264 case NFA_SPRINT: /* \P */ | |
4470 | 3265 result = !VIM_ISDIGIT(c) && ptr2cells(reginput) == 1; |
4444 | 3266 ADD_POS_NEG_STATE(t->state); |
3267 break; | |
3268 | |
3269 case NFA_WHITE: /* \s */ | |
3270 result = vim_iswhite(c); | |
3271 ADD_POS_NEG_STATE(t->state); | |
3272 break; | |
3273 | |
3274 case NFA_NWHITE: /* \S */ | |
3275 result = c != NUL && !vim_iswhite(c); | |
3276 ADD_POS_NEG_STATE(t->state); | |
3277 break; | |
3278 | |
3279 case NFA_DIGIT: /* \d */ | |
3280 result = ri_digit(c); | |
3281 ADD_POS_NEG_STATE(t->state); | |
3282 break; | |
3283 | |
3284 case NFA_NDIGIT: /* \D */ | |
3285 result = c != NUL && !ri_digit(c); | |
3286 ADD_POS_NEG_STATE(t->state); | |
3287 break; | |
3288 | |
3289 case NFA_HEX: /* \x */ | |
3290 result = ri_hex(c); | |
3291 ADD_POS_NEG_STATE(t->state); | |
3292 break; | |
3293 | |
3294 case NFA_NHEX: /* \X */ | |
3295 result = c != NUL && !ri_hex(c); | |
3296 ADD_POS_NEG_STATE(t->state); | |
3297 break; | |
3298 | |
3299 case NFA_OCTAL: /* \o */ | |
3300 result = ri_octal(c); | |
3301 ADD_POS_NEG_STATE(t->state); | |
3302 break; | |
3303 | |
3304 case NFA_NOCTAL: /* \O */ | |
3305 result = c != NUL && !ri_octal(c); | |
3306 ADD_POS_NEG_STATE(t->state); | |
3307 break; | |
3308 | |
3309 case NFA_WORD: /* \w */ | |
3310 result = ri_word(c); | |
3311 ADD_POS_NEG_STATE(t->state); | |
3312 break; | |
3313 | |
3314 case NFA_NWORD: /* \W */ | |
3315 result = c != NUL && !ri_word(c); | |
3316 ADD_POS_NEG_STATE(t->state); | |
3317 break; | |
3318 | |
3319 case NFA_HEAD: /* \h */ | |
3320 result = ri_head(c); | |
3321 ADD_POS_NEG_STATE(t->state); | |
3322 break; | |
3323 | |
3324 case NFA_NHEAD: /* \H */ | |
3325 result = c != NUL && !ri_head(c); | |
3326 ADD_POS_NEG_STATE(t->state); | |
3327 break; | |
3328 | |
3329 case NFA_ALPHA: /* \a */ | |
3330 result = ri_alpha(c); | |
3331 ADD_POS_NEG_STATE(t->state); | |
3332 break; | |
3333 | |
3334 case NFA_NALPHA: /* \A */ | |
3335 result = c != NUL && !ri_alpha(c); | |
3336 ADD_POS_NEG_STATE(t->state); | |
3337 break; | |
3338 | |
3339 case NFA_LOWER: /* \l */ | |
3340 result = ri_lower(c); | |
3341 ADD_POS_NEG_STATE(t->state); | |
3342 break; | |
3343 | |
3344 case NFA_NLOWER: /* \L */ | |
3345 result = c != NUL && !ri_lower(c); | |
3346 ADD_POS_NEG_STATE(t->state); | |
3347 break; | |
3348 | |
3349 case NFA_UPPER: /* \u */ | |
3350 result = ri_upper(c); | |
3351 ADD_POS_NEG_STATE(t->state); | |
3352 break; | |
3353 | |
3354 case NFA_NUPPER: /* \U */ | |
3355 result = c != NUL && !ri_upper(c); | |
3356 ADD_POS_NEG_STATE(t->state); | |
3357 break; | |
3358 | |
4482 | 3359 case NFA_MOPEN + 0: |
3360 case NFA_MOPEN + 1: | |
3361 case NFA_MOPEN + 2: | |
3362 case NFA_MOPEN + 3: | |
3363 case NFA_MOPEN + 4: | |
3364 case NFA_MOPEN + 5: | |
3365 case NFA_MOPEN + 6: | |
3366 case NFA_MOPEN + 7: | |
3367 case NFA_MOPEN + 8: | |
3368 case NFA_MOPEN + 9: | |
3369 /* handled below */ | |
3370 break; | |
3371 | |
3372 case NFA_SKIP_CHAR: | |
3373 case NFA_ZSTART: | |
3374 /* TODO: should not happen? */ | |
3375 break; | |
3376 | |
4444 | 3377 default: /* regular character */ |
4482 | 3378 /* TODO: put this in #ifdef later */ |
3379 if (t->state->c < -256) | |
3380 EMSGN("INTERNAL: Negative state char: %ld", t->state->c); | |
4444 | 3381 result = (no_Magic(t->state->c) == c); |
4482 | 3382 |
4444 | 3383 if (!result) |
3384 result = ireg_ic == TRUE | |
3385 && 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
|
3386 #ifdef FEAT_MBYTE |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3387 /* 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
|
3388 * 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
|
3389 * character uses NFA_COMPOSING above. */ |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3390 if (result && enc_utf8 && !ireg_icombine |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3391 && n != utf_char2len(c)) |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3392 result = FALSE; |
55bcaa1d2749
updated for version 7.3.1011
Bram Moolenaar <bram@vim.org>
parents:
4525
diff
changeset
|
3393 #endif |
4444 | 3394 ADD_POS_NEG_STATE(t->state); |
3395 break; | |
3396 } | |
3397 | |
3398 } /* for (thislist = thislist; thislist->state; thislist++) */ | |
3399 | |
3400 /* The first found match is the leftmost one, but there may be a | |
3401 * longer one. Keep running the NFA, but don't start from the | |
3402 * beginning. Also, do not add the start state in recursive calls of | |
3403 * nfa_regmatch(), because recursive calls should only start in the | |
3404 * first position. */ | |
3405 if (match == FALSE && start->c == NFA_MOPEN + 0) | |
3406 { | |
3407 #ifdef ENABLE_LOG | |
3408 fprintf(log_fd, "(---) STARTSTATE\n"); | |
3409 #endif | |
3410 addstate(nextlist, start, m, n, listid + 1, &match); | |
3411 } | |
3412 | |
3413 #ifdef ENABLE_LOG | |
3414 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); | |
3415 for (i = 0; i< thislist->n; i++) | |
3416 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); | |
3417 fprintf(log_fd, "\n"); | |
3418 #endif | |
3419 | |
3420 nextchar: | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3421 /* 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
|
3422 * finish. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3423 if (n != 0) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3424 reginput += n; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3425 else if (go_to_nextline) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3426 reg_nextline(); |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3427 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3428 break; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3429 } |
4444 | 3430 |
3431 #ifdef ENABLE_LOG | |
3432 if (log_fd != stderr) | |
3433 fclose(log_fd); | |
3434 log_fd = NULL; | |
3435 #endif | |
3436 | |
3437 theend: | |
3438 /* Free memory */ | |
3439 vim_free(list[0].t); | |
3440 vim_free(list[1].t); | |
3441 vim_free(list[2].t); | |
3442 list[0].t = list[1].t = list[2].t = NULL; | |
3443 if (listids != NULL) | |
3444 vim_free(listids); | |
3445 #undef ADD_POS_NEG_STATE | |
4460 | 3446 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 3447 fclose(debug); |
3448 #endif | |
3449 | |
3450 return match; | |
3451 } | |
3452 | |
3453 /* | |
3454 * Try match of "prog" with at regline["col"]. | |
3455 * Returns 0 for failure, number of lines contained in the match otherwise. | |
3456 */ | |
3457 static long | |
3458 nfa_regtry(start, col) | |
3459 nfa_state_T *start; | |
3460 colnr_T col; | |
3461 { | |
3462 int i; | |
3463 regsub_T sub, m; | |
3464 #ifdef ENABLE_LOG | |
3465 FILE *f; | |
3466 #endif | |
3467 | |
3468 reginput = regline + col; | |
3469 need_clear_subexpr = TRUE; | |
3470 | |
3471 #ifdef ENABLE_LOG | |
3472 f = fopen(LOG_NAME, "a"); | |
3473 if (f != NULL) | |
3474 { | |
3475 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n"); | |
3476 fprintf(f, " =======================================================\n"); | |
3477 #ifdef DEBUG | |
3478 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); | |
3479 #endif | |
3480 fprintf(f, "\tInput text is \"%s\" \n", reginput); | |
3481 fprintf(f, " =======================================================\n\n\n\n\n\n\n"); | |
3482 nfa_print_state(f, start, 0); | |
3483 fprintf(f, "\n\n"); | |
3484 fclose(f); | |
3485 } | |
3486 else | |
3487 EMSG(_("Could not open temporary log file for writing ")); | |
3488 #endif | |
3489 | |
3490 if (REG_MULTI) | |
3491 { | |
3492 /* Use 0xff to set lnum to -1 */ | |
3493 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3494 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3495 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3496 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3497 } | |
3498 else | |
3499 { | |
3500 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP); | |
3501 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP); | |
3502 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP); | |
3503 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP); | |
3504 } | |
3505 | |
3506 if (nfa_regmatch(start, &sub, &m) == FALSE) | |
3507 return 0; | |
3508 | |
3509 cleanup_subexpr(); | |
3510 if (REG_MULTI) | |
3511 { | |
3512 for (i = 0; i < NSUBEXP; i++) | |
3513 { | |
3514 reg_startpos[i] = sub.startpos[i]; | |
3515 reg_endpos[i] = sub.endpos[i]; | |
3516 } | |
3517 | |
3518 if (reg_startpos[0].lnum < 0) | |
3519 { | |
3520 reg_startpos[0].lnum = 0; | |
3521 reg_startpos[0].col = col; | |
3522 } | |
3523 if (reg_endpos[0].lnum < 0) | |
3524 { | |
3525 reg_endpos[0].lnum = reglnum; | |
3526 reg_endpos[0].col = (int)(reginput - regline); | |
3527 } | |
3528 else | |
3529 /* Use line number of "\ze". */ | |
3530 reglnum = reg_endpos[0].lnum; | |
3531 } | |
3532 else | |
3533 { | |
3534 for (i = 0; i < NSUBEXP; i++) | |
3535 { | |
3536 reg_startp[i] = sub.start[i]; | |
3537 reg_endp[i] = sub.end[i]; | |
3538 } | |
3539 | |
3540 if (reg_startp[0] == NULL) | |
3541 reg_startp[0] = regline + col; | |
3542 if (reg_endp[0] == NULL) | |
3543 reg_endp[0] = reginput; | |
3544 } | |
3545 | |
3546 return 1 + reglnum; | |
3547 } | |
3548 | |
3549 /* | |
3550 * Match a regexp against a string ("line" points to the string) or multiple | |
3551 * lines ("line" is NULL, use reg_getline()). | |
3552 * | |
3553 * Returns 0 for failure, number of lines contained in the match otherwise. | |
3554 */ | |
3555 static long | |
3556 nfa_regexec_both(line, col) | |
3557 char_u *line; | |
3558 colnr_T col; /* column to start looking for match */ | |
3559 { | |
3560 nfa_regprog_T *prog; | |
3561 long retval = 0L; | |
3562 int i; | |
3563 | |
3564 if (REG_MULTI) | |
3565 { | |
3566 prog = (nfa_regprog_T *)reg_mmatch->regprog; | |
3567 line = reg_getline((linenr_T)0); /* relative to the cursor */ | |
3568 reg_startpos = reg_mmatch->startpos; | |
3569 reg_endpos = reg_mmatch->endpos; | |
3570 } | |
3571 else | |
3572 { | |
3573 prog = (nfa_regprog_T *)reg_match->regprog; | |
3574 reg_startp = reg_match->startp; | |
3575 reg_endp = reg_match->endp; | |
3576 } | |
3577 | |
3578 /* Be paranoid... */ | |
3579 if (prog == NULL || line == NULL) | |
3580 { | |
3581 EMSG(_(e_null)); | |
3582 goto theend; | |
3583 } | |
3584 | |
3585 /* If the start column is past the maximum column: no need to try. */ | |
3586 if (ireg_maxcol > 0 && col >= ireg_maxcol) | |
3587 goto theend; | |
3588 | |
3589 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ | |
3590 if (prog->regflags & RF_ICASE) | |
3591 ireg_ic = TRUE; | |
3592 else if (prog->regflags & RF_NOICASE) | |
3593 ireg_ic = FALSE; | |
3594 | |
3595 #ifdef FEAT_MBYTE | |
3596 /* If pattern contains "\Z" overrule value of ireg_icombine */ | |
3597 if (prog->regflags & RF_ICOMBINE) | |
3598 ireg_icombine = TRUE; | |
3599 #endif | |
3600 | |
3601 regline = line; | |
3602 reglnum = 0; /* relative to line */ | |
3603 | |
3604 nstate = prog->nstate; | |
3605 | |
3606 for (i = 0; i < nstate; ++i) | |
3607 { | |
3608 prog->state[i].id = i; | |
3609 prog->state[i].lastlist = 0; | |
3610 prog->state[i].visits = 0; | |
3611 prog->state[i].lastthread = NULL; | |
3612 } | |
3613 | |
3614 retval = nfa_regtry(prog->start, col); | |
3615 | |
3616 theend: | |
3617 return retval; | |
3618 } | |
3619 | |
3620 /* | |
3621 * Compile a regular expression into internal code for the NFA matcher. | |
3622 * Returns the program in allocated space. Returns NULL for an error. | |
3623 */ | |
3624 static regprog_T * | |
3625 nfa_regcomp(expr, re_flags) | |
3626 char_u *expr; | |
3627 int re_flags; | |
3628 { | |
3629 nfa_regprog_T *prog; | |
4458 | 3630 size_t prog_size; |
4444 | 3631 int *postfix; |
3632 | |
3633 if (expr == NULL) | |
3634 return NULL; | |
3635 | |
3636 #ifdef DEBUG | |
3637 nfa_regengine.expr = expr; | |
3638 #endif | |
3639 | |
3640 init_class_tab(); | |
3641 | |
3642 if (nfa_regcomp_start(expr, re_flags) == FAIL) | |
3643 return NULL; | |
3644 | |
3645 /* Space for compiled regexp */ | |
3646 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate_max; | |
3647 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); | |
3648 if (prog == NULL) | |
3649 goto fail; | |
3650 vim_memset(prog, 0, prog_size); | |
3651 | |
3652 /* Build postfix form of the regexp. Needed to build the NFA | |
3653 * (and count its size) */ | |
3654 postfix = re2post(); | |
3655 if (postfix == NULL) | |
3656 goto fail; /* Cascaded (syntax?) error */ | |
3657 | |
3658 /* | |
3659 * In order to build the NFA, we parse the input regexp twice: | |
3660 * 1. first pass to count size (so we can allocate space) | |
3661 * 2. second to emit code | |
3662 */ | |
3663 #ifdef ENABLE_LOG | |
3664 { | |
3665 FILE *f = fopen(LOG_NAME, "a"); | |
3666 | |
3667 if (f != NULL) | |
3668 { | |
3669 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); | |
3670 fclose(f); | |
3671 } | |
3672 } | |
3673 #endif | |
3674 | |
3675 /* | |
3676 * PASS 1 | |
3677 * Count number of NFA states in "nstate". Do not build the NFA. | |
3678 */ | |
3679 post2nfa(postfix, post_ptr, TRUE); | |
3680 state_ptr = prog->state; | |
3681 | |
3682 /* | |
3683 * PASS 2 | |
3684 * Build the NFA | |
3685 */ | |
3686 prog->start = post2nfa(postfix, post_ptr, FALSE); | |
3687 if (prog->start == NULL) | |
3688 goto fail; | |
3689 | |
3690 prog->regflags = regflags; | |
3691 prog->engine = &nfa_regengine; | |
3692 prog->nstate = nstate; | |
3693 #ifdef ENABLE_LOG | |
3694 nfa_postfix_dump(expr, OK); | |
3695 nfa_dump(prog); | |
3696 #endif | |
3697 | |
3698 out: | |
3699 vim_free(post_start); | |
3700 post_start = post_ptr = post_end = NULL; | |
3701 state_ptr = NULL; | |
3702 return (regprog_T *)prog; | |
3703 | |
3704 fail: | |
3705 vim_free(prog); | |
3706 prog = NULL; | |
3707 #ifdef ENABLE_LOG | |
3708 nfa_postfix_dump(expr, FAIL); | |
3709 #endif | |
3710 #ifdef DEBUG | |
3711 nfa_regengine.expr = NULL; | |
3712 #endif | |
3713 goto out; | |
3714 } | |
3715 | |
3716 | |
3717 /* | |
3718 * Match a regexp against a string. | |
3719 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). | |
3720 * Uses curbuf for line count and 'iskeyword'. | |
3721 * | |
3722 * Return TRUE if there is a match, FALSE if not. | |
3723 */ | |
3724 static int | |
3725 nfa_regexec(rmp, line, col) | |
3726 regmatch_T *rmp; | |
3727 char_u *line; /* string to match against */ | |
3728 colnr_T col; /* column to start looking for match */ | |
3729 { | |
3730 reg_match = rmp; | |
3731 reg_mmatch = NULL; | |
3732 reg_maxline = 0; | |
3733 reg_line_lbr = FALSE; | |
3734 reg_buf = curbuf; | |
3735 reg_win = NULL; | |
3736 ireg_ic = rmp->rm_ic; | |
3737 #ifdef FEAT_MBYTE | |
3738 ireg_icombine = FALSE; | |
3739 #endif | |
3740 ireg_maxcol = 0; | |
3741 return (nfa_regexec_both(line, col) != 0); | |
3742 } | |
3743 | |
3744 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
3745 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
3746 | |
3747 static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); | |
3748 | |
3749 /* | |
3750 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. | |
3751 */ | |
3752 static int | |
3753 nfa_regexec_nl(rmp, line, col) | |
3754 regmatch_T *rmp; | |
3755 char_u *line; /* string to match against */ | |
3756 colnr_T col; /* column to start looking for match */ | |
3757 { | |
3758 reg_match = rmp; | |
3759 reg_mmatch = NULL; | |
3760 reg_maxline = 0; | |
3761 reg_line_lbr = TRUE; | |
3762 reg_buf = curbuf; | |
3763 reg_win = NULL; | |
3764 ireg_ic = rmp->rm_ic; | |
3765 #ifdef FEAT_MBYTE | |
3766 ireg_icombine = FALSE; | |
3767 #endif | |
3768 ireg_maxcol = 0; | |
3769 return (nfa_regexec_both(line, col) != 0); | |
3770 } | |
3771 #endif | |
3772 | |
3773 | |
3774 /* | |
3775 * Match a regexp against multiple lines. | |
3776 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3777 * Uses curbuf for line count and 'iskeyword'. | |
3778 * | |
3779 * Return zero if there is no match. Return number of lines contained in the | |
3780 * match otherwise. | |
3781 * | |
3782 * Note: the body is the same as bt_regexec() except for nfa_regexec_both() | |
3783 * | |
3784 * ! Also NOTE : match may actually be in another line. e.g.: | |
3785 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like | |
3786 * | |
3787 * +-------------------------+ | |
3788 * |a | | |
3789 * |b | | |
3790 * |c | | |
3791 * | | | |
3792 * +-------------------------+ | |
3793 * | |
3794 * then nfa_regexec_multi() returns 3. while the original | |
3795 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. | |
3796 * | |
3797 * FIXME if this behavior is not compatible. | |
3798 */ | |
3799 static long | |
3800 nfa_regexec_multi(rmp, win, buf, lnum, col, tm) | |
3801 regmmatch_T *rmp; | |
3802 win_T *win; /* window in which to search or NULL */ | |
3803 buf_T *buf; /* buffer in which to search */ | |
3804 linenr_T lnum; /* nr of line to start looking for match */ | |
3805 colnr_T col; /* column to start looking for match */ | |
3806 proftime_T *tm UNUSED; /* timeout limit or NULL */ | |
3807 { | |
3808 reg_match = NULL; | |
3809 reg_mmatch = rmp; | |
3810 reg_buf = buf; | |
3811 reg_win = win; | |
3812 reg_firstlnum = lnum; | |
3813 reg_maxline = reg_buf->b_ml.ml_line_count - lnum; | |
3814 reg_line_lbr = FALSE; | |
3815 ireg_ic = rmp->rmm_ic; | |
3816 #ifdef FEAT_MBYTE | |
3817 ireg_icombine = FALSE; | |
3818 #endif | |
3819 ireg_maxcol = rmp->rmm_maxcol; | |
3820 | |
4503 | 3821 return nfa_regexec_both(NULL, col); |
4444 | 3822 } |
3823 | |
3824 #ifdef DEBUG | |
3825 # undef ENABLE_LOG | |
3826 #endif |