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