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