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